package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SinDeed;
import com.ruoyi.system.mapper.SinDeedMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SinSincerityMapper;
import com.ruoyi.system.domain.SinSincerity;
import com.ruoyi.system.service.ISinSincerityService;

/**
 * 诚信事迹Service业务层处理
 *
 * @author buke
 * @date 2023-03-10
 */
@Service
public class SinSincerityServiceImpl extends ServiceImpl<SinSincerityMapper,SinSincerity> implements ISinSincerityService
{
    @Autowired
    private SinSincerityMapper sinSincerityMapper;
    @Autowired
    private SysUserMapper userMapper ;
    @Autowired
    private SinDeedMapper deedMapper ;

    /**
     * 查询诚信事迹
     *
     * @param id 诚信事迹主键
     * @return 诚信事迹
     */
    @Override
    public SinSincerity selectSinSincerityById(Long id)
    {
        return sinSincerityMapper.selectSinSincerityById(id);
    }

    /**
     * 查询诚信事迹列表
     *
     * @param sinSincerity 诚信事迹
     * @return 诚信事迹
     */
    @Override
    public List<SinSincerity> selectSinSincerityList(SinSincerity sinSincerity)
    {
        return sinSincerityMapper.selectSinSincerityList(sinSincerity);
    }

    /**
     * 新增诚信事迹
     *
     * @param sinSincerity 诚信事迹
     * @return 结果
     */
    @Override
    public int insertSinSincerity(SinSincerity sinSincerity)
    {
        sinSincerity.setCreateTime(DateUtils.getNowDate());
        if (!Objects.isNull(sinSincerity.getMainUserId()) && sinSincerity.getMainUserId() != 0L){
            SysUser user = userMapper.selectUserById(sinSincerity.getMainUserId());
            sinSincerity.setMainUser(user.getNickName());
        }
        if (!Objects.isNull(sinSincerity.getSincerityTypeId()) && sinSincerity.getSincerityTypeId() != 0L){
            SinDeed deed = deedMapper.selectSinDeedById(sinSincerity.getSincerityTypeId());
            sinSincerity.setSincerityNumber(deed.getScoreNumber());
            sinSincerity.setSincerityTypeName(deed.getName());
            sinSincerity.setSincerityType(deed.getType());
        }
        return sinSincerityMapper.insertSinSincerity(sinSincerity);
    }

    /**
     * 修改诚信事迹
     *
     * @param sinSincerity 诚信事迹
     * @return 结果
     */
    @Override
    public int updateSinSincerity(SinSincerity sinSincerity)
    {
        sinSincerity.setUpdateTime(DateUtils.getNowDate());
        if (!Objects.isNull(sinSincerity.getMainUserId()) && sinSincerity.getMainUserId() != 0L){
            SysUser user = userMapper.selectUserById(sinSincerity.getMainUserId());
            sinSincerity.setMainUser(user.getNickName());
        }
        if (!Objects.isNull(sinSincerity.getSincerityTypeId()) && sinSincerity.getSincerityTypeId() != 0L){
            SinDeed deed = deedMapper.selectSinDeedById(sinSincerity.getSincerityTypeId());
            sinSincerity.setSincerityNumber(deed.getScoreNumber());
            sinSincerity.setSincerityTypeName(deed.getName());
            sinSincerity.setSincerityType(deed.getType());
        }
        return sinSincerityMapper.updateSinSincerity(sinSincerity);
    }

    /**
     * 批量删除诚信事迹
     *
     * @param ids 需要删除的诚信事迹主键
     * @return 结果
     */
    @Override
    public int deleteSinSincerityByIds(Long[] ids)
    {
        return sinSincerityMapper.deleteSinSincerityByIds(ids);
    }

    /**
     * 删除诚信事迹信息
     *
     * @param id 诚信事迹主键
     * @return 结果
     */
    @Override
    public int deleteSinSincerityById(Long id)
    {
        return sinSincerityMapper.deleteSinSincerityById(id);
    }
}
